﻿//Copyright (c) 2010, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace yaTDL.Core.Converters
{
    /// <summary>
    /// An IValueConverter that converts a bool value to a Visibility.
    /// Use this to bind a control's Visibility property to a bool.
    /// </summary>
    public class BoolVisibilityConverter:IValueConverter
    {
        /// <summary>
        /// Converts a bool value into a Visbility.
        /// </summary>
        /// <param name="value">The bool value to be converted.</param>
        /// <param name="targetType">The target type of the conversion. (It will be assumed that it is Visibility)</param>
        /// <param name="parameter">
        /// The conversion parameter. Set this 'invert' if you want the bool value to be inverted. 
        /// (true --> Visibility.Collapsed instead of true --> Visbility.Visible)
        /// </param>
        /// <param name="culture">The CultureInfo for the conversion. This parameter will be ignored.</param>
        /// <returns>Returns a System.Windows.Visibility value based on the values of 'value' and 'parameter'.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool || value is bool?)    //check if the given value is a bool and the conversion can be executed.
            {
                bool b = (bool)value;

                if (parameter is string)
                {
                    if ((string)parameter == "invert")
                    {
                        b = !b; 
                    }
                }
                
                return b ? Visibility.Visible : Visibility.Collapsed;
            }
            else        //if value wasn't bool, the converter was not used correctly and the original value will be returned.
            { 
                return value; 
            }
        }

        /// <summary>
        /// Converts a Visbility value back into a bool value.
        /// </summary>
        /// <param name="value">The Visbility value to be converted.</param>
        /// <param name="targetType">The target type of the conversion. (It will be assumed that it is bool)</param>
        /// <param name="parameter">
        /// The conversion parameter. Set this 'invert' if you want the bool value to be inverted. 
        /// (Visibility.Collapsed/Visibility.Hidden ---> true instead of Visibility.Collapsed/Visibility.Hidden ---> false)
        /// </param>
        /// <param name="culture">The CultureInfo for the conversion. This parameter will be ignored.</param>
        /// <returns>Returns a bool value based on the values of 'value' and 'parameter'.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Visibility)            //check if the given value is a Visbility and the conversion can be executed.
            {
                Visibility v = (Visibility)value;
                bool b = false;

                switch (v)
                {
                    case Visibility.Collapsed:                        
                    case Visibility.Hidden:
                        b = false;
                        break;
                    case Visibility.Visible:
                        b = true;
                        break;                    
                }


                if (parameter is string)                //check if the given parameter is a string
                {
                    if ((string)parameter == "invert")
                    {
                        b = !b;                         //if the parameter if 'invert', invert the value of b
                    }
                }

                return b;

            }
            else        //if the given value wasn't a Visibility, the converter was not used correctly and the original value will be returned.
            {
                return value;
            }

        }
    }
}
